﻿using System;
using System.Drawing;

namespace ZYCommon
{
    

    public class RectangleObject
    {
        protected int intHeight = 0;
        protected int intLeft = 0;
        protected int intTop = 0;
        protected int intWidth = 0;

        public bool Contains(Rectangle vRect)
        {
            Rectangle rectangle = new Rectangle(this.intLeft, this.intTop, this.intWidth, this.intHeight);
            return rectangle.Contains(vRect);
        }

        public bool Contains(int x, int y)
        {
            return ((((x > this.intLeft) && (x < (this.intLeft + this.intWidth))) && (y > this.intTop)) && (y < (this.intTop + this.intHeight)));
        }

        public bool Contains(int x, int y, int vWidth, int vHeight)
        {
            Rectangle rectangle = new Rectangle(this.intLeft, this.intTop, this.intWidth, this.intHeight);
            Rectangle rect = new Rectangle(x, y, vWidth, vHeight);
            return rectangle.Contains(rect);
        }

        public static Rectangle ConvertToRectangle(RectangleF rf)
        {
            return new Rectangle((int) rf.Left, (int) rf.Top, (int) rf.Width, (int) rf.Height);
        }

        public static int GetAcmeIndex(Rectangle vRect, Point p)
        {
            if (!vRect.IsEmpty)
            {
                if (p.Y == vRect.Y)
                {
                    if (p.X == vRect.X)
                    {
                        return 0;
                    }
                    if (p.X == vRect.Right)
                    {
                        return 1;
                    }
                }
                else if (p.Y == vRect.Bottom)
                {
                    if (p.X == vRect.Right)
                    {
                        return 2;
                    }
                    if (p.X == vRect.X)
                    {
                        return 3;
                    }
                }
            }
            return -1;
        }

        public static Point GetAcmePos(Rectangle vRect, int AcmeIndex)
        {
            switch (AcmeIndex)
            {
                case 0:
                    return vRect.Location;

                case 1:
                    return new Point(vRect.Right, vRect.Y);

                case 2:
                    return new Point(vRect.Right, vRect.Bottom);

                case 3:
                    return new Point(vRect.X, vRect.Bottom);
            }
            return Point.Empty;
        }

        public Point GetBorderPoint(int iPos, int iSplit)
        {
            Point empty = Point.Empty;
            if (iSplit > 0)
            {
                iPos = iPos % (iSplit * 4);
                if (iPos < 0)
                {
                    iPos += iSplit * 4;
                }
                if ((iPos >= 0) && (iPos < iSplit))
                {
                    empty.X = this.intLeft + ((this.intWidth * iPos) / iSplit);
                    empty.Y = this.intTop;
                }
                else
                {
                    if ((iPos >= iSplit) && (iPos < (iSplit * 2)))
                    {
                        empty.X = this.intLeft + this.intWidth;
                        empty.Y = this.intTop + ((this.intHeight * (iPos - iSplit)) / iSplit);
                        return empty;
                    }
                    if ((iPos >= (iSplit * 2)) && (iPos < (iSplit * 3)))
                    {
                        empty.X = this.intLeft + ((this.intWidth * ((iSplit * 3) - iPos)) / iSplit);
                        empty.Y = this.intTop + this.intHeight;
                    }
                    else
                    {
                        empty.X = this.intLeft;
                        empty.Y = this.intTop + ((this.intHeight * ((iSplit * 4) - iPos)) / iSplit);
                    }
                }
            }
            return empty;
        }

        public static Rectangle GetRectangle(Point p1, Point p2)
        {
            return GetRectangle(p1.X, p1.Y, p2.X, p2.Y);
        }

        public static Rectangle GetRectangle(int x1, int y1, int x2, int y2)
        {
            Rectangle empty = Rectangle.Empty;
            if (x1 < x2)
            {
                empty.X = x1;
                empty.Width = x2 - x1;
            }
            else
            {
                empty.X = x2;
                empty.Width = x1 - x2;
            }
            if (y1 < y2)
            {
                empty.Y = y1;
                empty.Height = y2 - y1;
            }
            else
            {
                empty.Y = y2;
                empty.Height = y1 - y2;
            }
            if (empty.Width < 1)
            {
                empty.Width = 1;
            }
            if (empty.Height < 1)
            {
                empty.Height = 1;
            }
            return empty;
        }

        public Rectangle Intersect(Rectangle vRect)
        {
            Rectangle rectangle = new Rectangle(this.intLeft, this.intTop, this.intWidth, this.intHeight);
            rectangle.Intersect(vRect);
            return rectangle;
        }

        public bool IntersectsWith(Rectangle vRect)
        {
            Rectangle rectangle = new Rectangle(this.intLeft, this.intTop, this.intWidth, this.intHeight);
            return rectangle.IntersectsWith(vRect);
        }

        public bool IntersectsWith(int x, int y, int vWidth, int vHeight)
        {
            Rectangle rectangle = new Rectangle(this.intLeft, this.intTop, this.intWidth, this.intHeight);
            Rectangle rect = new Rectangle(x, y, vWidth, vHeight);
            return rectangle.IntersectsWith(rect);
        }

        public bool isEmpty()
        {
            return ((((this.intLeft == 0) && (this.intTop == 0)) && (this.intWidth == 0)) && (this.intHeight == 0));
        }

        public void MoveStep(int dx, int dy)
        {
            this.intLeft += dx;
            this.intTop += dy;
        }

        public void MoveTo(int x, int y)
        {
            this.intLeft = x;
            this.intTop = y;
        }

        public void MoveTo2(int x, int y)
        {
            this.intLeft = x - this.intWidth;
            this.intTop = y - this.intHeight;
        }

        public void ReSize(int vWidth, int vHeight)
        {
            this.intWidth = vWidth;
            this.intHeight = vHeight;
        }

        public void SetRect(int x1, int y1, int x2, int y2)
        {
            if (x1 < x2)
            {
                this.intLeft = x1;
                this.intWidth = x2 - x1;
            }
            else
            {
                this.intLeft = x2;
                this.intWidth = x1 - x2;
            }
            if (y1 < y2)
            {
                this.intTop = y1;
                this.intHeight = y2 - y1;
            }
            else
            {
                this.intTop = y2;
                this.intHeight = y1 - y2;
            }
        }

        public Rectangle Union(RectangleObject vRect)
        {
            if (vRect == null)
            {
                return Rectangle.Empty;
            }
            Rectangle b = new Rectangle(this.intLeft, this.intTop, this.intWidth, this.intHeight);
            return Rectangle.Union(vRect.InnerRect, b);
        }

        public Rectangle Union(Rectangle vRect)
        {
            Rectangle a = new Rectangle(this.intLeft, this.intTop, this.intWidth, this.intHeight);
            return Rectangle.Union(a, vRect);
        }

        public int Bottom
        {
            get
            {
                return (this.intTop + this.intHeight);
            }
            set
            {
                this.intTop = value - this.intHeight;
            }
        }

        public int Height
        {
            get
            {
                return this.intHeight;
            }
            set
            {
                this.intHeight = value;
            }
        }

        public Rectangle InnerRect
        {
            get
            {
                return new Rectangle(this.intLeft, this.intTop, this.intWidth, this.intHeight);
            }
            set
            {
                this.intLeft = value.Left;
                this.intTop = value.Top;
                this.intWidth = value.Width;
                this.intHeight = value.Height;
            }
        }

        public int Left
        {
            get
            {
                return this.intLeft;
            }
            set
            {
                this.intLeft = value;
            }
        }

        public int Right
        {
            get
            {
                return (this.intLeft + this.intWidth);
            }
            set
            {
                this.intLeft = value - this.intWidth;
            }
        }

        public int Top
        {
            get
            {
                return this.intTop;
            }
            set
            {
                this.intTop = value;
            }
        }

        public int Width
        {
            get
            {
                return this.intWidth;
            }
            set
            {
                this.intWidth = value;
            }
        }
    }
}

